import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
from matplotlib import animation

from TopFrameParser import TopFrameParser
from DirMonitor import DirMonitor

fig = plt.figure()
ax = plt.axes()
line, = ax.plot([], [], label="loading", lw=1, linestyle='--')
line2, = ax.plot([], [], label="avg", lw=2)
ax.legend(handles=[line, line2], loc='lower right')
dm = DirMonitor('./assets')
df = pd.DataFrame()


def update_data():
    global df
    added_files = dm.watch_changes()
    if len(added_files) > 0:
        frames = TopFrameParser.parse_files(added_files)
        df = df.append(frames)
        df = df.sort_index()
        print(added_files)
        print(df)


def update(i):
    global df
    update_data()
    xdata = df.index
    ydata = df['cpu_usage']

    line.set_data(xdata, ydata)
    # line.axes.axis([None, None, ydata.min(), ydata.max()])
    fig.autofmt_xdate()
    return line,


if __name__ == '__main__':
    anim = animation.FuncAnimation(fig, update, frames=200, interval=1000, blit=False)
    plt.show()
